﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RestSharp;
using tDonkeySite.Models;
using eDonkeyClassLibrary;

namespace tDonkeySite.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Save(string user)
        {
            var client = new RestClient("http://tdonekyapi.yudapps.com/");
            //var client = new RestClient("http://localhost:45312/");
            var request = new RestRequest("api/User/Save", Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.AddParameter("user", user);
            //string cookies = this.ControllerContext.HttpContext.Request.Cookies
            StringBuilder cookies = new StringBuilder();
            string[] requestedCookies = { "rankNum", "Courses" };
            foreach (string c in this.ControllerContext.HttpContext.Request.Cookies.AllKeys)
            {
                if (requestedCookies.Contains(c))
                {
                    cookies.Append(c);
                    cookies.Append("=");
                    cookies.Append(this.ControllerContext.HttpContext.Request.Cookies[c].Value);
                    cookies.Append(";");
                }

            }
            string scookies = cookies.ToString();
            request.AddParameter("url", scookies);
            IRestResponse<int> response = client.Execute<int>(request);
            //check if response is -1...

            return null;
        }

        public ActionResult Open(string user)
        {
            var client = new RestClient("http://tdonekyapi.yudapps.com/");
            //var client = new RestClient("http://localhost:45312/");
            var request = new RestRequest("api/User/Open", Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.AddParameter("user", user);
            IRestResponse<User> response = client.Execute<User>(request);
            string url = response.Data.URL;
            /* we don't need this, js code will do the work:            
            string[] cookies = url.Split(';');
            Dictionary<string,string> cookiesDic = new Dictionary<string,string>();
            char [] sep = {'='};
            foreach (string c in cookies)
            {
                string[] keyAndValue = c.Split(sep,2);
                if (keyAndValue != null && keyAndValue[0] != null && keyAndValue.Length == 2)
                {
                    cookiesDic[keyAndValue[0]] = keyAndValue[1];
                }
            }
            this.ControllerContext.HttpContext.Request.Cookies.Clear();
            foreach (string c in cookiesDic.Keys)
            {
                //this.ControllerContext.HttpContext.Request.Cookies.Remove(c);
                this.ControllerContext.HttpContext.Request.Cookies.Set(new System.Web.HttpCookie(c, cookiesDic[c]));
            }
            */
            return Json(new { url }, null, JsonRequestBehavior.AllowGet);
        }
    }
}
